<?php
/**
 * employe/show-contrat-menu.phtml
 */
?>
<?php

use RHX\Helper\RHDateTime;
?>
<?php
// variables
$exception = $this->exception;
if (!is_null($exception)):
    ?>
    <?php echo $exception->message; ?>
<?php else: ?>
    <?php
    /**
     * @var RHX\Model\Object\Employe Employé
     */
    $employe	 = $this->employe;
    $debutContrat	 = new RHDateTime($employe->contrat_debut);
    if ($employe->contrat_fin == '0000-00-00') {
	$employe->contrat_fin = $debutContrat->date();
    }
    $contratDebut	 = new RHDateTime($employe->contrat_fin);
    $contratFinRef	 = new RHDateTime($employe->contrat_fin);
    $contratDebut->modify('next day');
    ?>
    <?php
    $etat		 = $this->etat;
    $nb		 = (int) substr($etat, 0, 1);
    $message	 = '';
    if ($nb != 0) {
	$message = substr($etat, 2);
    }
    ?>
        <div>
        <div class="empl-full content-head" style='margin-bottom: 10px;'>
    	<h4>Nouveau Contrat pour l'employé <b><?php echo $employe->prenom . ' ' . $employe->nom1 ?></b></h4>
        </div>
        <div class="empl-full saumon">
    	Type du contrat : <a href="#" id="c-contrat_type" data-type="select"></a>
        </div>
        <div class='contrat-container'>
    	<div class="empl-full gris<?php echo ($nb != 0) ? ' contrat-erreur' : '' ?>" style='margin-left: -5px;'>
    	    Début : <a href="#" id="c-contrat_debut" data-type="combodate"><?php echo 'N/A'; //$contratDebut->date('d/m/Y');      ?></a>
    	</div>
    	<!--div class="empl-full gris">
    	    Fin : <a href="#" id="c-contrat_fin" data-type="combodate">00/00/0000</a>
    	</div-->
    	<div style="height: 2em; margin: 5px auto;"></div>
    	<div class="empl-full gris-beige">
    	    Poste : <a href="#" id="c-poste" data-type="select"></a>
    	</div>
    	<div class="empl-full saumon">
    	    Collège : <a href='#' id='c-college' data-type='select'></a>
    	</div>
    	<div class="empl-full saumon">
    	    Niveau : <a href='#' id='c-niveau' data-type='select'></a>
    	</div>
    	<div class="empl-full saumon">
    	    Echelon : <a href='#' id='c-echelon' data-type='select'></a>
    	</div>
        </div>
        <div class='contrat-container'>
    	<!--div class="empl-full gris" style='margin-left: -5px;'>
    	    Motif de départ : <a href="#" id="c-contrat_motif" data-type="select"></a>
    	</div-->
    	<div class='empl-full' style='margin-left: -5px; border: none'>&nbsp;</div>
    	<div style="height: 2em; margin: 5px auto;"></div>
    	<div class="empl-full jaune">
    	    Horaire Hebdomadaire : <a href="#" id="c-hedbo" data-type="text">00,00</a> Heure(s)
    	</div>
    	<div class="empl-full jaune">
    	    Taux Horaire : <a href="#" id="c-taux" data-type="text">00,00</a> €
    	</div>
    	<!--        	<div class="empl-full gris">
    			    Début : <a href="#" id="c-avenant_debut" data-type='combodate'><?php echo date('d/m/Y'); ?></a>
    	</div>
    	<div class="empl-full gris">
    	    Fin : <a href="#" id="c-avenant_fin" data-type='combodate'>00/00/0000</a>
    			</div>-->
        </div>
        <div>
    	<div class="empl-full">
    	    <span id='contrat-msg'><?php echo $message; ?></span>
    	    <button class="btn btn-inverse" id="btn-annuler" style="float: right; margin: 3px; text-transform: capitalize;">Annuler</button>&nbsp;
    	    <button class="btn btn-success hide" id="btn-valider" style="float: right; margin: 3px; text-transform: capitalize;<?php echo ($nb != 0) ? ' display: none' : ''; ?>">Valider</button>
    	</div>
        </div>
    </div>
    <form id="contratForm" action="#" method="post">
        <input type="hidden" id="form-matricule" name="matricule" value="<?php echo $employe->matricule; ?>">
        <input type="hidden" id="form-contrat_debut" name="contrat_debut" value="">
        <input type="hidden" id="form-contrat_fin" name="contrat_fin" value="0000-00-00">
        <input type="hidden" id="form-contrat_motif" name="contrat_motif" value="0">
        <input type="hidden" id="form-contrat_type" name="contrat_type" value="0">
        <input type="hidden" id="form-poste" name="poste" value="0">
        <input type="hidden" id="form-avenant_debut" name="avenant_debut" value="0">
        <input type="hidden" id="form-avenant_fin" name="avenant_fin" value="0">
        <input type="hidden" id="form-hebdo" name="hebdo" value="0.0">
        <input type="hidden" id="form-taux" name="taux" value="0.0">
        <input type="hidden" id="form-niveau" name="niveau" value="1">
        <input type="hidden" id="form-echelon" name="echelon" value="1">
        <input type="hidden" id="form-college" name="college" value="1">
    </form>
    <script type="text/javascript">
            $(function() {
        	// editable
        	$('#c-contrat_type').editable({
        	    title: "Type du Contrat",
        	    source: "<?php echo $this->url('RHX/default', array('controller'	 => 'employe', 'action'	 => 'contratTypeList')) ?>",
        	    validate: function(value) {
        		$('#form-contrat_type').val(value);
        	    }
        	});
        	$('#c-contrat_motif').editable({
        	    title: "Motif de départ",
        	    source: "<?php echo $this->url('RHX/default', array('controller'	 => 'employe', 'action'	 => 'jsonMotifList')) ?>",
        	    validate: function(value) {
        		$('#form-contrat_motif').val(value);
        	    }
        	});
        	$('#c-contrat_debut').editable({
        	    title: "Début du Contrat",
        	    format: 'DD/MM/YYYY',
        	    template: 'D / MMMM / YYYY',
        	    placement: 'right',
        	    params: function(params) {
        		value = params.value;
        		date = moment(value, ['YYYY-MM-DD', 'DD/MM/YYYY']);
        		params.value = date.format('YYYY-MM-DD');
        		return params;
        	    },
        	    validate: function(value) {
        		if (value.length == 0) {
        		    $('#btn-valider').hide('normal');
        		    $('#form-contrat_debut').val('0000-00-00');
        		} else {
        		    date = moment(value, ['YYYY-MM-DD', 'DD/MM/YYYY']);
        		    if (date.isValid()) {
        			//$('#btn-valider').show('normal');
    				    $('#form-contrat_debut').val(date.format('YYYY-MM-DD'));
    				    $(this).parent().removeClass('contrat-erreur');
    				    $('#contrat-msg').text('');
    				} else {
    				    $('#btn-valider').hide('normal');
    				    $('#form-contrat_debut').val('0000-00-00');
    				    $(this).parent().removeClass('contrat-erreur').addClass('contrat-erreur');
    				}
            				if (!isGT(date.format('YYYY-MM-DD'), '<?php echo $contratDebut->date(); ?>')) {
                    					showError("Attention!<br>La date de début du contrat est antérieur à la date du <?php echo $contratDebut->date('d/m/Y'); ?>");
    				    $('#btn-valider').hide('normal');
    				} else {
    				    $('#btn-valider').show('normal');
    				}
    			    }
    			    // traitement de l'affichage
    			    val = value.year() + '-' + (parseInt(value.month()) + 1) + '-' + value.date();
    			    $('#form-contrat_debut').val(val);
    			}
    		    });
    		    $('#c-contrat_fin').editable({
    			title: "Fin de Contrat",
    			format: 'DD/MM/YYYY',
    			template: 'D / MMMM / YYYY',
    			placement: 'right',
    			params: function(params) {
    			    value = params.value;
    			    date = moment(value, ['YYYY-MM-DD', 'DD/MM/YYYY']);
    			    params.value = date.format('YYYY-MM-DD');
    			    return params;
    			},
    			validate: function(value) {
    			    if (value.length == 0) {
    				$('#form-contrat_fin').val('0000-00-00');
    			    } else {
    				date = moment(value, ['YYYY-MM-DD', 'DD/MM/YYYY']);
    				$('#form-contrat_fin').val(date.format('YYYY-MM-DD'));
    			    }
    			}
    		    });
    		    $('#c-hedbo').editable({
    			title: "Contrat Horaire Hebdomadaire",
    			validate: function(value) {
    			    $('#form-hebdo').val(value);
    			},
    			tpl: "<input type='text' onclick='select();'>"
    		    });
    		    $('#c-taux').editable({
    			title: "Taux Horaire",
    			validate: function(value) {
    			    $('#form-taux').val(value);
    			},
    			tpl: "<input type='text' onclick='select();'>"
    		    });
    		    $('#c-avenant_debut').editable({
    			title: "Début de l'avenant",
    			format: 'DD/MM/YYYY',
    			template: 'D / MMMM / YYYY',
    			placement: 'left',
    			params: function(params) {
    			    value = params.value;
    			    date = moment(value, ['YYYY-MM-DD', 'DD/MM/YYYY']);
    			    params.value = date.format('YYYY-MM-DD');
    			    return params;
    			},
    			validate: function(value) {
    			    var val = value.year() + '-' + (parseInt(value.month()) + 1) + '-' + value.date();
    			    $('#form-avenant_debut').val(val);
    			}
    		    });
    		    $('#c-avenant_fin').editable({
    			title: "Début de l'avenant",
    			format: 'DD/MM/YYYY',
    			template: 'D / MMMM / YYYY',
    			placement: 'left',
    			params: function(params) {
    			    value = params.value;
    			    date = moment(value, ['YYYY-MM-DD', 'DD/MM/YYYY']);
    			    params.value = date.format('YYYY-MM-DD');
    			    return params;
    			},
    			validate: function(value) {
    			    var val = value.year() + '-' + (parseInt(value.month()) + 1) + '-' + value.date();
    			    $('#form-avenant_fin').val(val);
    			}
    		    });
    		    $('#c-poste').editable({
    			title: "Poste",
    			source: "<?php echo $this->url('RHX/default', array('controller'	 => 'employe', 'action'	 => 'jsonpostelist')) ?>",
    			validate: function(value) {
    			    $('#form-poste').val(value);
    			    poste = value;
    			    $('#data-poste_id').val(value);
    			    ajax = $.ajax({
    				type: 'post',
    				url: '<?php echo $this->url('RHX/default', array('controller'	 => 'employe', 'action'	 => 'getPosteInfo')) ?>',
    				data: '&id=' + poste
    			    });
    			    ajax.done(function(result) {
    				info = result.info;
    				$('#c-college').text(info.college);
    				$('#c-niveau').text(info.niveau);
    				$('#c-echelon').text(info.echelon);
    				// changement des données
    				$('#form-college').val(info.college_value);
    				$('#form-niveau').val(info.niveau_value);
    				$('#form-echelon').val(info.echelon_value);
    			    });
    			}
    		    });
	<?php
	$niveau = array();
	for ($i = 1; $i < 6; $i++) {
	    $niveau[$i] = 'Niveau ' . \RHX\Helper\Number::getRomanNumber($i);
	}
	?>
    		    $('#c-niveau').editable({
    			title: 'Niveau',
    			source: '<?php echo json_encode($niveau); ?>',
    			validate: function(value) {
    			    $('#form-niveau').val(value);
    			}
    		    });

	    <?php
	    $echelon = array();
	    for ($i = 1; $i < 5; $i++) {
		$echelon[$i] = "Echelon $i";
	    }
	    ?>
        	$('#c-echelon').editable({
        	    title: "Echelon",
        	    source: '<?php echo json_encode($echelon); ?>',
        	    validate: function(value) {
        		$('#form-echelon').val(value);
        	    }
        	});

            	$('#c-college').editable({
        	    title: "post",
        	    source: '<?php echo $this->url('RHX/default', array('controller'	 => 'employe', 'action'	 => 'collegeList')) ?>',
        	    validate: function(value) {
        		$('#form-college').val(value);
        	    }
        	});

    $('#btn-annuler').click(function(e) {
    	e.preventDefault();
    	$('#dialog-contrat').hide('normal');
        });
        $('#btn-valider').click(function(e) {
    	e.preventDefault();
    	ajax = $.ajax({
    	    type: 'post',
    	    url: '<?php echo $this->url('RHX/default', array('controller'	 => 'employe', 'action'	 => 'contratAdd')) ?>',
    	    data: $('#contratForm').serialize()
    	});
    	ajax.done(function(result) {
    	    // rafraichir la page
    	    window.location.reload();
    	});
    	$('#btn-annuler').click();
        });
        });
        </script>
<?php endif; ?>
<style type='text/css'>
    .empl-full{
	width: 99%;
	/*margin: auto;*/
    }
    .contrat-container{
	width: 49%;
	display: inline-table;
	padding: 0px;
    }
    #contrat-msg{
	line-height: 32px;
	color: #ed6767;
	text-transform: none;
    }
    .contrat-erreur{
	box-shadow: 0px 0px 5px 0px #ff4343;
	border: solid 1px #de3838;
    }
</style>